import { func } from "joi"
import db from "../db/index.js"
// function dateFormat(date) {
//     date = date.toLocaleDateString()
//     return date
// }
// Date.prototype.toJSON = function () { return dateFormat(this) }

export function getDataFromAcl(req, res) {
    //定义sql查询语句
    //TODO:后端反序列化:


    const place = `acl_` + req.body.place
    const sql = `select * from ` + place + ` where date between ? and ? and id%?=0`
    //调用db.query()
    db.query(sql, [req.body.startDate, req.body.endDate, req.body.interval], (err, results) => {
        if (err) {
            return res.cc(err)
        }
        // results.date = dateFormat(results.date)
        res.send({
            data: {
                status: 0,
                message: '读取数据成功!',
                data: results,
            }
        }
        )
    })
}

export function getDataFromDft(req, res) {
    //定义sql查询语句
    const place = `dft_` + req.body.place
    const sql = `select * from ` + place + ` where date between ? and ? and id%?=0`
    //调用db.query()
    db.query(sql, [req.body.startDate, req.body.endDate, req.body.interval], (err, results) => {
        if (err) {
            return res.cc(err)
        }
        res.send({
            data: {
                status: 0,
                message: '读取数据成功!',
                data: results,
            }
        })
    })
}

export function getDataFromTurb(req, res) {
    //定义sql查询语句
    const place = `turb_` + req.body.place
    const sql = `select * from ` + place + ` where date between ? and ? and id%?=0`
    //调用db.query()
    db.query(sql, [req.body.startDate, req.body.endDate, req.body.interval], (err, results) => {
        if (err) {
            return res.cc(err)
        }
        res.send({
            data: {
                status: 0,
                message: '读取数据成功!',
                data: results,
            }
        })
    })
}

export function getDataFromPas(req, res) {
    //定义sql查询语句
    const place = `pas_` + req.body.place
    const sql = `select * from ` + place + ` where date between ? and ? and id%?=0`
    //调用db.query()
    db.query(sql, [req.body.startDate, req.body.endDate, req.body.interval], (err, results) => {
        if (err) {
            return res.cc(err)
        }
        res.send({
            data: {
                status: 0,
                message: '读取数据成功!',
                data: results,
            }
        })
    })
}

export function getDataFromWs(req, res) {
    //定义sql查询语句
    const place = `ws_` + req.body.place
    const sql = `select * from ` + place + ` where date between ? and ? and id%?=0`
    //调用db.query()
    db.query(sql, [req.body.startDate, req.body.endDate, req.body.interval], (err, results) => {
        if (err) {
            return res.cc(err)
        }
        res.send({
            data: {
                status: 0,
                message: '读取数据成功!',
                data: results,
            }
        })
    })
}

export function getDataFromVis(req, res) {
    //定义sql查询语句
    const place = `vis_` + req.body.place
    const sql = `select * from ` + place + ` where date between ? and ? and id%?=0`
    //调用db.query()
    db.query(sql, [req.body.startDate, req.body.endDate, req.body.interval], (err, results) => {
        if (err) {
            return res.cc(err)
        }
        res.send({
            data: {
                status: 0,
                message: '读取数据成功!',
                data: results,
            }
        })
    })
}